/*
 * NieuwSpelFrame.java
 *
 * Created on 29 april 2008, 13:04
 */
package be.hogent.iii.project.client.gui.gameroom;

import be.hogent.iii.project.client.netwerk.Connectie;
import be.hogent.iii.project.common.bericht.SpelWijzigingBericht;
import be.hogent.iii.project.common.game.objecten.Spel;
import com.thoughtworks.xstream.XStream;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/**
 *
 * @author  Caribou
 */
public class NieuwSpelFrame extends javax.swing.JFrame {

    private Connectie connectie;
    private static NieuwSpelFrame frame;

    /** Creates new form NieuwSpelFrame */
    private NieuwSpelFrame() {
        initComponents();
        this.setTitle("Nieuw spel");
        setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    public static synchronized NieuwSpelFrame getNieuwSpelFrame() {
        if (frame == null) {
            frame = new NieuwSpelFrame();
        }
        return frame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        aantalSpelerTxt = new javax.swing.JTextField();
        maakSpelBtn = new javax.swing.JButton();
        aantalSpelersLbl = new javax.swing.JLabel();
        naamLbl = new javax.swing.JLabel();
        naamTxt = new javax.swing.JTextField();
        annulerenBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        aantalSpelerTxt.setBackground(new java.awt.Color(102, 0, 0));
        aantalSpelerTxt.setFont(new java.awt.Font("Tahoma", 1, 11));
        aantalSpelerTxt.setForeground(new java.awt.Color(204, 153, 0));
        aantalSpelerTxt.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        aantalSpelerTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aantalSpelerTxtActionPerformed(evt);
            }
        });

        maakSpelBtn.setBackground(new java.awt.Color(102, 0, 0));
        maakSpelBtn.setForeground(new java.awt.Color(204, 153, 0));
        maakSpelBtn.setText("Maak spel");
        maakSpelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maakSpelBtnActionPerformed(evt);
            }
        });

        aantalSpelersLbl.setForeground(new java.awt.Color(255, 255, 255));
        aantalSpelersLbl.setText("Maximum deelnemers: ");

        naamLbl.setForeground(new java.awt.Color(255, 255, 255));
        naamLbl.setText("Spelnaam:");

        naamTxt.setBackground(new java.awt.Color(102, 0, 0));
        naamTxt.setFont(new java.awt.Font("Tahoma", 1, 11));
        naamTxt.setForeground(new java.awt.Color(204, 153, 0));
        naamTxt.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        naamTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                naamTxtActionPerformed(evt);
            }
        });

        annulerenBtn.setBackground(new java.awt.Color(102, 0, 0));
        annulerenBtn.setForeground(new java.awt.Color(204, 153, 0));
        annulerenBtn.setText("Annuleren");
        annulerenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annulerenBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(maakSpelBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(aantalSpelersLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(naamLbl, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(annulerenBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(naamTxt)
                    .addComponent(aantalSpelerTxt))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(naamLbl)
                    .addComponent(naamTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aantalSpelersLbl)
                    .addComponent(aantalSpelerTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maakSpelBtn)
                    .addComponent(annulerenBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void naamTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_naamTxtActionPerformed
        maakSpelBtnActionPerformed(evt);
}//GEN-LAST:event_naamTxtActionPerformed

    private void maakSpelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maakSpelBtnActionPerformed
        String naam = naamTxt.getText();
        String aantalString = aantalSpelerTxt.getText();
        try {
            int aantalSpelers = Integer.parseInt(aantalString);
            Spel nieuwSpel = new Spel(aantalSpelers, naam, GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler());
            nieuwSpel.addDeelnemer(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler());
            
            SpelWijzigingBericht wijziging = new SpelWijzigingBericht(nieuwSpel);

            XStream xStream = new XStream();
            GameRoomGuiFrame.getGameRoomGuiFrame().getConnectie().stuurBericht(xStream.toXML(wijziging));

            this.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Maximum aantal spelers moet een getal zijn. ", "Fout", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        
}//GEN-LAST:event_maakSpelBtnActionPerformed

    private void aantalSpelerTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aantalSpelerTxtActionPerformed
        maakSpelBtnActionPerformed(evt);
    }//GEN-LAST:event_aantalSpelerTxtActionPerformed

    private void annulerenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annulerenBtnActionPerformed
        this.dispose();
    }//GEN-LAST:event_annulerenBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NieuwSpelFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aantalSpelerTxt;
    private javax.swing.JLabel aantalSpelersLbl;
    private javax.swing.JButton annulerenBtn;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton maakSpelBtn;
    private javax.swing.JLabel naamLbl;
    private javax.swing.JTextField naamTxt;
    // End of variables declaration//GEN-END:variables
    public Connectie getConnectie() {
        return connectie;
    }

    public void setConnectie(Connectie connectie) {
        this.connectie = connectie;
    }

}
